using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace WAG.Menu
{
    public class LogoScreen : DrawableGameComponent
    {
        SpriteBatch sprite_batch;
        
        Texture2D logo;
        Rectangle texture_rectangle;
        Matrix screen_transform;
        string logo_path;
        float alpha;

        #region Constructors

        public LogoScreen(Game game, string logo_path)
            : base(game)
        {
            alpha = 0.0f;
            this.logo_path = logo_path;
        }

        #endregion

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            
            base.Initialize();
        }

        Thread t = null;
        protected override void LoadContent()
        {
            screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / SpaceBattleGame.DEFAULT_SCR_WIDTH, 
                GraphicsDevice.Viewport.Height / SpaceBattleGame.DEFAULT_SCR_HEIGHT, 1.0f);

            logo = Game.Content.Load<Texture2D>(logo_path);
            texture_rectangle = new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2, 
                Game.GraphicsDevice.Viewport.Height / 2 - logo.Height / 2, logo.Width, logo.Height);

            t = new Thread(() =>
            {
                //Game.Content.Load<Texture2D>("Menu/g_title_png");
                Game.Content.Load<SpriteFont>("Fonts/OCRAExtended30");
                Game.Content.Load<SpriteFont>("Fonts/OCRAExtended16");

                Game.Content.Load<Texture2D>("UI/WhitePixel");
                //Game.Content.Load<Texture2D>("Menu/loading_bar_png");
                Game.Content.Load<Texture2D>("Menu/glowing_point");
                Game.Content.Load<SpriteFont>("Fonts/OCRAExtended30");

                var bg_path = new[] {
                    "Backgrounds/DefaulsBackground/"};

                for (int i = 0; i < bg_path.Length; i++)
                    for (int j = 0; j < 6; j++)
                        Game.Content.Load<Texture2D>(bg_path[i] + ((CubeMapFace)i).ToString());
            });

            t.Start();

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            alpha += (dt * 2.0f);

            if (alpha > 2.0f && t.ThreadState != ThreadState.Running) StateManager.SelectMenu(Game);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            sprite_batch.Draw(logo, texture_rectangle, Color.White * alpha);

            sprite_batch.End();

            base.Draw(gameTime);
        }
    }
}
